package fragments;

/**
 * Created by Administrator on 2015/1/31.
 */
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.game.demo409.demo409.R;
import com.yalantis.pulltorefresh.library.PullToRefreshView;


import java.util.ArrayList;
import java.util.LinkedList;




import fragments.FragmentAbstract.FindingInFmBase;
import views.LoadMoreListView;

/**
 * Created by neokree on 16/12/14.
 */
public class FindingAll extends FindingInFmBase{
    private static final String ARG_POSITION = "position";

    private  static final int REFRESH_DELAY = 2000;
    private static final int LOAD_DELAY = 2000;

    private  int position;

    private PullToRefreshView mPullToRefreshView;

    private LoadMoreListView mListView;

    private ArrayAdapter<String> mAdapter;

    private LinkedList<String> data ;

    private static Handler handler = new Handler();

    private boolean isPrepared = false;

    public  static  FindingAll newInstance(int position) {
       FindingAll fa = new FindingAll();
       Bundle b = new Bundle();
       b.putInt(ARG_POSITION,position);
       fa.setArguments(b);
       return fa;
   }
    @Override

    public  void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       position = getArguments().getInt(ARG_POSITION);
       data = new LinkedList<String>();
        data.add("活动1");
        data.add("活动2");
        data.add("活动3");
        data.add("活动4");
        data.add("活动5");
        data.add("活动6");
        data.add("活动7");
        data.add("活动8");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

       View findingall = inflater.inflate(R.layout.finddingall_fm,container,false);

       mListView = (LoadMoreListView)findingall.findViewById(R.id.all_list);

       // all_list.setAdapter(new FindingallAdapter(list,getActivity()));
       mAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_expandable_list_item_1,data);
        mListView.setOnGetMoreListener(new LoadMoreListView.OnGetMoreListener() {
            @Override
            public void onGetMore() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        data.add("加载+");
                        mAdapter.notifyDataSetChanged();
                        mListView.getMoreComplete();
                    }
                },LOAD_DELAY);
            }
        });

        mListView.setAdapter(mAdapter);
        mPullToRefreshView = (PullToRefreshView)findingall.findViewById(R.id.pull_to_refresh_all);
        mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public  void  onRefresh() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        data.addFirst("新活动");
                        mAdapter.notifyDataSetChanged();
                         mPullToRefreshView.setRefreshing(false);
                    }
                },REFRESH_DELAY);

            }

        });

       mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

           }
       });

        isPrepared = true;
        mListView.performGetMore();
        FragmentLoading();

        return findingall;
    }
    @Override
    protected  void FragmentLoading() {
        if(!isPrepared||!isVisible) {
            return;
        }
        //Fragment可见就刷新

        mPullToRefreshView.setRefreshing(true);

        new Thread (new Runnable() {
            @Override
            public void run() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        data.addFirst("新活动");
                        mAdapter.notifyDataSetChanged();
                        mPullToRefreshView.setRefreshing(false);
                    }
                },REFRESH_DELAY);
            }
        }).start();
    }
}
